package GUI.gameWin;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Insets;
import java.awt.LayoutManager;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import GUI.gameWinOld.CardButton;

public class CardHorizontalLayout implements LayoutManager {
	private int cshpx;
	private int minWidth = 0;
	private int preferredWidth = 0, preferredHeight = 110;
	private boolean sizeUnknown = true;

	public CardHorizontalLayout() {
		this(12);
	}

	public CardHorizontalLayout(int cshpx) {
		this.cshpx = cshpx;
	}

	private void setSizes(Container parent) {
		int compCount = parent.getComponentCount();
		int cardCount = 0;
		for (int i = 0; i < compCount; i++) {
			Component c = parent.getComponent(i);
			if (c instanceof CardButton) {
				cardCount++;
			}
		}
		minWidth = 104 + (cardCount-1) * cshpx;
		preferredWidth = minWidth;

	}

	@Override
	public void addLayoutComponent(String arg0, Component arg1) {

	}

	@Override
	public void layoutContainer(Container parent) {
		if (sizeUnknown)
			setSizes(parent);
		int compCount = parent.getComponentCount();
		int cardsWidth = (compCount-1)*cshpx + 84;
		int horOS;
		if(parent.getWidth()>cardsWidth)
			horOS = (parent.getWidth()-cardsWidth)/2;
		else
			horOS = 10;
		int verOS=18;

		CardButton lastCard = null;
		for (int i = 0; i < compCount; i++) {
			Component c = parent.getComponent(i);
			if(c instanceof CardButton) {
				lastCard = (CardButton) c;
				Dimension d = c.getPreferredSize();
				c.setBounds(horOS, verOS, cshpx, d.height);
				horOS+=cshpx;
			}
		}
		if (lastCard != null) {
			if(lastCard.getCard().getFakeCard()!=null&&lastCard.getOwner()==null) {
				CardButton tempButton = new CardButton(null, null, lastCard.getCard().getRealCard(), true, true);
				JButton button = new JButton();
				button.setBorder(null);
				button.setContentAreaFilled(false);
				button.setIcon(new ImageIcon(((ImageIcon)tempButton.getIcon()).getImage().getScaledInstance(3*lastCard.getPreferredSize().width/4, 3*lastCard.getPreferredSize().height/4, Image.SCALE_SMOOTH)));
				parent.add(button);
				button.setBounds(horOS-cshpx+lastCard.getPreferredSize().width/8, verOS+lastCard.getPreferredSize().height/8, button.getPreferredSize().width, button.getPreferredSize().height);
				parent.setComponentZOrder(button, 0);
			}
			lastCard.setBounds(horOS-cshpx, verOS, lastCard.getPreferredSize().width, lastCard.getPreferredSize().height);
			
		}
	}

	@Override
	public Dimension minimumLayoutSize(Container parent) {
		return preferredLayoutSize(parent);
	}

	@Override
	public Dimension preferredLayoutSize(Container parent) {
		Dimension dim = new Dimension(0, 0);
		setSizes(parent);

		Insets insets = parent.getInsets();
		dim.width = preferredWidth + insets.left + insets.right;
		dim.height = preferredHeight + insets.bottom + insets.top;

		sizeUnknown = false;
		return dim;
	}

	@Override
	public void removeLayoutComponent(Component child) {

	}

}
